package com.liu.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author liuchengjie
 */
@SuppressWarnings({"all"})
public class SocketTcpServer02 {
    public static void main(String[] args) throws IOException {
        //在本机9999端口监听，等待链接,要求端口没被占用
        ServerSocket serverSocket = new ServerSocket(9999);
        //当有客户端链接时，会返回对象socket,没有链接，会阻塞
        Socket socket = serverSocket.accept();
        System.out.println("socket的运行信息");
        System.out.println(socket.getClass());

        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int readLen = 0;
        while ((readLen = inputStream.read(bytes)) != -1) {
            System.out.println(new String(bytes,0,readLen));
        }

        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("收到消息，hello,client".getBytes());
        //结束标记
        socket.shutdownOutput();
        inputStream.close();
        socket.close();
        serverSocket.close();
        outputStream.close();
    }
}
